package cdu.javaendwork.dao;

import java.sql.*;

public class BaseDao {

    // 解耦  连接数据库的基本信息 pet-adoption-system petadopt
    String driver="com.mysql.cj.jdbc.Driver";
//    String url="jdbc:mysql://localhost:3306/pet-adoption-system?useSSL=true&serverTimezone=Asia/Shanghai";
    String url="jdbc:mysql://localhost:3306/petadopt?useSSL=true&serverTimezone=Asia/Shanghai";
    String dbUsername="root";
//    String dbPassword="abc123";
    String dbPassword="123456";
//    String dbPassword="root";
//    String url="jdbc:mysql://localhost:3306/pet-adoption-system?useSSL=true&serverTimezone=Asia/Shanghai";
//    String dbUsername="root";
//
//    String dbPassword="abc123";




    protected Connection conn=null;
    protected Statement stmt=null;
    protected PreparedStatement pstmt=null;
    protected ResultSet rs=null;

    public BaseDao(){
        connect();
    }


    public void connect(){

        try{
            // 加载驱动程序
            Class.forName(driver).newInstance();

            // 创建数据库连接
            conn= DriverManager.getConnection(url,dbUsername,dbPassword);

        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void close(){
        try {
            if(stmt!=null&&!stmt.isClosed()){
                stmt.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
